<?php
/*
 * vip会员中心
 */
class VipuserController extends Uuch_BaseController {

    public function init() {
        parent::init();        
    }

    public function indexAction() 
    {
        $userId = @$this->currentUser()->Id;
        if ($userId) {
            $this->view->user = $this->currentUser();
            $this->view->account = Application_Model_UserAccount::addAccount($userId);
            $this->view->cart = Application_Model_Cart::current();
            $this->view->email = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_EMAIL,$userId);
            $userItem = Application_Model_User::load($userId);
            $this->view->emailOld = $userItem->State;

            $this->view->pic = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_PIC,$userId);
            $this->view->userinfo = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_USERINFO,$userId);

            $this->view->order = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_ORDER,$userId);
            $this->view->orderOld = Application_Model_Order::getCountByUserId($userId);

            $this->view->recharge = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_RECHARGE,$userId);
            $this->view->rechargeOld = Application_Model_RechargeBill::getCountByUserId($userId);

            $this->view->visiting = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_VISITING,$userId);
            $this->view->visitingOld = Application_Model_VisitingServer::getCountByUserId($userId);

            $this->view->waybill = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_WAYBILL,$userId);
            $this->view->waybillOld = Application_Model_wayBillItem::getCountByUserId($userId);

            $this->view->weixin = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_WEIXIN,$userId);
            $this->view->weibo = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_WEIBO,$userId);
            $this->view->facebook = Application_Model_Task::getTaskComplete(Application_Model_Task::TASK_FACEBOOK,$userId);
            $this->view->IsReceive = Application_Model_StoreItem::getByUserIdBarcode($userId, Application_Model_StoreItem::User_Barcode);
            $couponItem = Application_Model_Coupon::getcouponByUserIdBatchNo($userId, "BirthdayReward");
            $isBirthday = 0;
            foreach ($couponItem as $couponVal) {
                if(date("Y",  strtotime("$couponVal->DateCreated")) == date("Y")){
                    $isBirthday = 1;
                }
            }
            $this->view->isBirthday = $isBirthday;
            $profile = Application_Model_Profile::getByUserId($userId);
            $birthday = 0;
            if($profile && $profile->Birthday != ""){
                if(date("m-d",  strtotime("$profile->Birthday"))== date("m-d"))
                {
                    $birthday = 1;
                }
            }
            $this->view->birthday = $birthday;
        }
        $this->_helper->layout->disableLayout();
    }

    public function birthdayAction()
    {
        $userId = $this->currentUser()->Id;
        $accountItem = Application_Model_UserAccount::addAccount($userId);
        $result = $accountItem->ReceiveBirthday();
        $this->json($result);
    }  
    
    public function verificationAction()
    {
        $userId = $this->currentUser()->Id;
        if (!$userId) {
            $this->redirect('login', 'user');
            return;
        }
        $taskCode = $this->getSafeParam("taskname");
        $taskDegree = 0;
        if($taskCode == "order"){
            $taskDegree = Application_Model_Order::getCountByUserId($userId);
        }else if($taskCode == "recharge"){
            $taskDegree = Application_Model_RechargeBill::getCountByUserId($userId);
        }else if($taskCode == "waybill"){
            $taskDegree = Application_Model_wayBillItem::getCountByUserId($userId);
        }else if($taskCode == "visiting"){
            $taskDegree = Application_Model_VisitingServer::getCountByUserId($userId);
        }else if($taskCode == "email"){
            $userItem = Application_Model_User::load($userId);
            $taskDegree = $userItem->State;
        }
        if ($taskDegree == 0) {
            $this->json(0);
            return ;
        }
        $taskItem = Application_Model_Task::getByTaskCode($taskCode);
        if(!$taskItem){
            $this->json(0);
            return ;
        }
        $completeItem = Application_Model_TaskComplete::getByTaskIdUserId($taskItem->Id, $userId);
        if($completeItem){
            $this->json(0);
            return ;
        }
        $accountItem = Application_Model_UserAccount::addAccount($userId);
        $accountItem->TaskExchangeScore($taskCode);
        $this->json(1);
    }
}
